package _interview75;

import java.util.Stack;

/**
 * 735. 小行星碰撞
 */
public class No735 {
    public int[] asteroidCollision(int[] asteroids) {
        Stack<Integer> stack = new Stack<>();
        label:
        for (int asteroid : asteroids) {
            if (stack.isEmpty()) {
                stack.push(asteroid);
                continue;
            }
            while (!stack.isEmpty()) {
                Integer peek = stack.peek();
                if (peek > 0 && asteroid < 0) {
                    if (peek < -asteroid) stack.pop();
                    else {
                        if (peek == -asteroid) stack.pop();
                        continue label;
                    }
                } else break;
            }
            stack.push(asteroid);
        }
        int size = stack.size();
        int[] answer = new int[size];
        for (int i = size - 1; i >= 0; i--) {
            answer[i] = stack.pop();
        }
        return answer;
    }
}
